<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","group_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_tools_groupman.php";
$FileTemplate  = "ais_tools_groupman_list.html";
$TableName     = "tblgroup";
$SubHeaderTitle= "Group Management";
$PageSize      = 10;
$listCheck     = 50;
$HTMLGridList  = "GroupGrid";
//************ FORM ACTION **************
	if (GetParam("Action","")=="Delete") {
		DeleteRecord ("txtCheck",$listCheck);
	}

//Initiate Database Table For Search;
$FCode = GetParam("groupCodeSearch","");
$FName = GetParam("groupSearch","");

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` ";

$SQLCount = "SELECT count(*) FROM `$TableName` ";

//Retrieve Parameter from search
if ($FCode) {
	$CustomWhere = " user like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FName) {
	$CustomWhere = " $TableName.group_id = '".$FName."'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;
}

//Build  WHERE 
if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";

$SQL = $SQL.$WHERE;
//print $SQL;
//Select User Group
$SQLGroup = "SELECT * FROM tblgroup ORDER BY group_name ";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ; 

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("groupblk",$DBMysql,$SQLGroup);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function DeleteRecord ($sequence_name,$listCheck) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$listCheck;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

?>
